﻿Imports System.Globalization
Imports System.Net
Imports <xmlns:nicolive="http://live.nicovideo.jp/">
Imports <xmlns:media="http://search.yahoo.com/mrss/">

' xmlns:media="http://search.yahoo.com/mrss/" xmlns:nicolive="http://live.nicovideo.jp/" xmlns:dc="http://purl.org/dc/elements/1.1/"

'<item>
'<title>カラーブリーフ推奨。の​集い。</title>
'<link>http://live.nicovideo.jp/watch/lv8290145</link>
'<guid isPermaLink="false">lv8290145</guid>
'<pubDate>Thu, 17 Dec 2009 18:29:13 +0900</pubDate>
'<description>好評コメ乞食中！
'obakeコールセンター
'skypeID:チョメ</description>
'<category>一般(その他)</category><category>凸待ち</category><media:thumbnail url="http://icon.nimg.jp/community/s/co141930.jpg?1259821439" width="64" height="64" />
'<dc:creator>死体置場/co141930</dc:creator>
'<nicolive:community_name>死体置場</nicolive:community_name>
'<nicolive:community_id>co141930</nicolive:community_id>
'<nicolive:num_res>0</nicolive:num_res>
'<nicolive:view>0</nicolive:view>
'<nicolive:member_only>false</nicolive:member_only>
'<nicolive:type>community</nicolive:type>
'<nicolive:owner_name>おばけ</nicolive:owner_name></item>
'<item>


Public Class NicoLiveFeed

    Public Property UserAgent As String

    Public Function GetLivePrograms(ByVal page As Integer) As IList(Of LiveProgram)
        Dim list = New List(Of LiveProgram)

        Dim uri = New Uri("http://live.nicovideo.jp/recent/rss?tab=all&sort=start&p=" & page.ToString(CultureInfo.InvariantCulture))

        Dim content = GetContent(uri)
        If content = "" Then
            Return list
        End If

        Try

            Dim doc = XDocument.Parse(content)
            Dim crawlingDate = Now

            For Each item In doc...<item>
                Dim p = New LiveProgram

                p.Id = Convert.ToInt32(item.<guid>.Value.Substring(2))
                p.Title = item.<title>.Value
                p.Description = item.<description>.Value
                p.VisitorCount = Convert.ToInt32(item.<nicolive:view>.Value)
                p.CommentCount = Convert.ToInt32(item.<nicolive:num_res>.Value)
                p.StartDateTime = DateTime.Parse(item.<pubDate>.Value)
                p.CasterName = item.<nicolive:owner_name>.Value
                p.IsMemberOnly = Convert.ToBoolean(item.<nicolive:member_only>.Value)
                p.CrawlingDateTime = crawlingDate

                For Each c In item...<category>
                    Select Case c.Value
                        Case "顔出し"
                            p.IsFace = True
                        Case "凸待ち"
                            p.IsTotsu = True
                        Case Else
                            p.OfficialTag = c.Value
                    End Select
                Next

                p.Station.Id = item.<nicolive:community_id>.Value
                p.Station.Name = item.<nicolive:community_name>.Value
                p.Station.IconUri = New Uri(item.<media:thumbnail>.@url)
                p.IsOnAir = True

                list.Add(p)
            Next
        Catch ex As Exception

        End Try

        Return list

    End Function

    Private Function GetContent(ByVal uri As Uri) As String

        Dim request = DirectCast(WebRequest.Create(uri), HttpWebRequest)
        request.UserAgent = UserAgent

        Dim content As String = ""
        Try
            Using response = request.GetResponse
                Using stream = New System.IO.StreamReader(response.GetResponseStream, System.Text.Encoding.UTF8)
                    content = stream.ReadToEnd
                End Using
            End Using
        Catch ex As Exception
#If DEBUG Then
            Console.WriteLine(ex.Message)
#End If
        End Try
        Return content

    End Function

End Class
